import pandas as pd

# 操作1：数据准备
# 读取评分信息
rnames = ['uid', 'mid', 'rating', 'timestamp']
ratings = pd.read_table('.\\ml-100k\\u.data', sep='\t', header=None, names=rnames)
ratings['Datetime'] = pd.to_datetime(ratings.timestamp, unit='s')
print(ratings.head())

# 读取用户信息
unames = ['uid', 'age', 'gender', 'occupation', 'zip']
users = pd.read_table('.\\ml-100k\\u.user', sep='|', header=None, names=unames)
print(users.head())

# 自然连接函数pd.merge(DataFrame a, DataFrame b)
frame = pd.merge(ratings, users)
print(frame.head())

# 读取电影信息
mnames = ['mid', 'title', 'date1', 'date2', 'url',
          'unknown', 'Action', 'Adventure', 'Animation',
          'Children', 'Comedy', 'Crime', 'Documentary', 'Drama',
          'Fantasy', 'Film-Noir', 'Horror', 'Musical',
          'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']
movies = pd.read_table('.\\ml-100k\\u.item', sep='|', header=None, names=mnames, encoding='ISO-8859-1')
print(movies.head())

frame = pd.merge(frame, movies)
print(frame.head())

# 至此，数据准备完毕

# 操作2：下面是观众的观影行为分析
# 首先查看性别评分差异
print(frame.rating.groupby(frame['gender']).mean())
# 不同年龄段评分差异(round中的参数（-1）表示四舍五入到十位)
print(frame.rating.groupby(frame['age'].apply(round, args=[-1])).mean())
# 结合年龄段和性别一起分析
print(frame.rating.groupby([frame['age'].apply(round, args=[-1]), frame['gender']]).mean())

# 操作3：电影的观众特征分析
# 按性别计算每部电影的平均得分
print(frame.rating.groupby([frame['gender'], frame['title']]).mean().sort_values(ascending=False))